/*
 * SList.h
 *
 *  Created on: Jun 27, 2011
 *      Author: apo
 */

#include "ListEl.h"
#include <iostream>
#ifndef SLIST_H_
#define SLIST_H_
using namespace std;

template<class type>
class List {
private:
	ListEl<type>* first;
	ListEl<type>* last;


public:
	//provisrium, wandern hoch dann

	List() {
		first = last = 0;
	}

	type* get(size_t index){
		ListEl<type>* iti;
		iti = first;
		for (size_t i = 0; i<index; i++){
			iti++;
		}
		return iti->getContent();
	}

	bool contains(const string& key) const {

		//durch die liste iterieren mit einem aktuellen element
		ListEl<type>* current = first;
		//das ende nicht erreicht
		while (current != 0) {
			if (current->getContent().key == key) {
				return true;
			}
			current = current->next;
		}
		return false;
	}

	//neues element ans ende anhängen, bzw als erstes element fast leer
	type append(type value) {
		cout<<"appendiere "<<endl;
		if (first == 0) {
			cout<<"erster "<<endl;

			first = new ListEl<type > (value);
			last = first;
			return value;
		} else {
			cout<<"nicht erster ;__; "<<endl;

			ListEl<type >* newLast = new ListEl<type > (value);
			last->append(newLast);
			last = newLast;
			return value;
		}

	}


	type& find(const string& key) throw () {
		//wieder mit dem aktuellen element durchiterieren
		ListEl<type >* current = first;
		while (current != 0) {
			if (current->getContent().key == key) {
				return current->getContent().value;
			}
		}
		throw myex; // wenn nicht gefunden
	}

	//stub
	bool isEmpty(){
		return NULL;
	}

	class myexception: public exception
	{
	  virtual const char* what() const throw()
	  {
	    return "Nicht vorhanden";
	  }
	} myex;



	virtual ~List() {
		//sich nach hinten "durchfressen"
		ListEl<type >* next = 0;
		while (this->first != 0) {
			next = first->next;
			delete first;http://z0r.de/2523
			first = next;
		}
	}

};

#endif /* SLIST_H_ */
